package com.ldj.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemSession;
import com.ldj.hibernate.AnnualVeriRepEndDate;
import com.ldj.hibernate.AnnualVeriRepEndDateDAO;
import com.ldj.hibernate.EmployerHistory;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.UnitInfoDAO;
import com.ldj.hibernate.UnitUser;
import com.ldj.hibernate.UnitUserApply;
import com.ldj.struts.form.EmployerInfoForm;

public class UnitInfoAction extends DispatchAction {

	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UnitInfoDAO unitInfoDao = new UnitInfoDAO(request, response);
		unitInfoDao.getSession().flush();
		unitInfoDao.getSession().clear();
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		if(uid.getUnitUser() == null){
			//String unitId = uid.getUnitUser().getEmpInfoId();
			List<UnitUserApply> list= unitInfoDao.findByUserId(loginId);
			if(list != null && list.size() > 0)
				return mapping.findForward("Applying");
			else{
				return mapping.findForward("addOrSelect");
			}
		}else{
			String unitId = uid.getUnitUser().getEmpInfoId();
			AnnualVeriRepEndDateDAO annuDao = new AnnualVeriRepEndDateDAO(request, response);
			AnnualVeriRepEndDate annuDate = annuDao.find();
			EmployerInfo info = unitInfoDao.findById(unitId);
			request.setAttribute("date", annuDate);
			request.setAttribute("uInfo", info);
			return mapping.findForward("detail");
		}
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			return mapping.findForward("add");	
	}
	
	public ActionForward toHistory(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			UnitInfoDAO unitInfoDao = new UnitInfoDAO(request, response);
			String loginId = SystemSession.getUnitUserId(request);
			UnitUser uid = unitInfoDao.findId(loginId);
			String unitId = uid.getUnitUser().getEmpInfoId();
			EmployerInfo info = unitInfoDao.findById(unitId);
			EmployerHistory ehInfo = new EmployerHistory();

			List<EmployerHistory> historyList = unitInfoDao.findAllfromYear(unitId, request.getParameter("empHistoryYear"));
			 if(historyList != null && historyList.size() > 0){
				 request.setAttribute("error","该年度的单位信息已经录入，不能重复录入！" );
				return mapping.findForward("error");
			 }
				ehInfo.setEmpHistoryYear(request.getParameter("empHistoryYear"));
				ehInfo.setEmpInfoAllWorkNum(info.getEmpInfoAllWorkNum());
				ehInfo.setEmpInfoContractedNum(info.getEmpInfoContractedNum());
				ehInfo.setEmpInfoCountryWorkNum(info.getEmpInfoCountryWorkNum());
				ehInfo.setEmpInfoEmpInjInsNum(info.getEmpInfoEmpInjInsNum());
				ehInfo.setEmpInfoEndowmentInsNum(info.getEmpInfoEndowmentInsNum());
				ehInfo.setEmpInfoGoonContractNum(info.getEmpInfoGoonContractNum());
				ehInfo.setEmpInfoId(unitId);
				ehInfo.setEmpInfoIfContract(info.getEmpInfoIfContract());
				ehInfo.setEmpInfoIfOrgDealDisputed(info.getEmpInfoIfOrgDealDisputed());
				ehInfo.setEmpInfoLowsalaLevel(info.getEmpInfoLowsalaLevel());
				ehInfo.setEmpInfoManHourRegu(info.getEmpInfoManHourRegu());
				ehInfo.setEmpInfoMaterityInsNum(info.getEmpInfoMaterityInsNum());
				ehInfo.setEmpInfoMedicalInsNum(info.getEmpInfoMedicalInsNum());
				ehInfo.setEmpInfoNcontractedNum(info.getEmpInfoNcontractedNum());
				ehInfo.setEmpInfoNoInsNum(info.getEmpInfoNoInsNum());
				ehInfo.setEmpInfoTownWorkNum(info.getEmpInfoTownWorkNum());
				ehInfo.setEmpInfoUnempInsNum(info.getEmpInfoUnempInsNum());
				unitInfoDao.saveHistory(ehInfo);
				return mapping.findForward("error");
	}
	
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriRepEndDateDAO annuDao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuDate = annuDao.find();
		Date a = new Date();
		Date b = annuDate.getAnnualVeriRepEndDate();
		if(a.before(b)) {
			UnitInfoDAO dao = new UnitInfoDAO(request, response);
			EmployerInfo info = dao.findById(request.getParameter("empInfoId"));
			request.setAttribute("eInfo", info);
			return mapping.findForward("toUpdate");
		}
		return new ActionForward("/unitinfo.do?method=detail", true);
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoForm eForm = (EmployerInfoForm)form;
		UnitInfoDAO dao = new UnitInfoDAO(request, response);
		EmployerInfo info = dao.findById(request.getParameter("eid"));
		info.setEmpInfoName(eForm.getEmpInfoName());
		info.setEmpInfoCode(eForm.getEmpInfoCode());
		info.setEmpInfoBusinessRegisOrgan(eForm.getEmpInfoBusinessRegisOrgan());
		info.setEmpInfoShopCard(eForm.getEmpInfoShopCard());
		info.setEmpInfoLandtaxUrl(eForm.getEmpInfoLandtaxUrl());
		info.setEmpInfoLandtaxRegisOrgan(eForm.getEmpInfoLandtaxRegisOrgan());
		info.setEmpInfoShopUrl(eForm.getEmpInfoShopUrl());
		info.setEmpInfoBankOfDeposit(eForm.getEmpInfoBankOfDeposit());
		info.setEmpInfoBankAccount(eForm.getEmpInfoBankAccount());
		info.setEmpInfoUnitType(eForm.getEmpInfoUnitType());
		info.setEmpInfoIndustry(eForm.getEmpInfoIndustry());
		info.setEmpInfoPlaceOrgan(eForm.getEmpInfoPlaceOrgan());
		info.setEmpInfoPlaceAddress(eForm.getEmpInfoPlaceAddress());
		info.setEmpInfoCorporateRepresent(eForm.getEmpInfoCorporateRepresent());
		info.setEmpInfoRepresentIdNum(eForm.getEmpInfoRepresentIdNum());
		info.setEmpInfoCellPhone(eForm.getEmpInfoCellPhone());
		info.setEmpInfoTelphone(eForm.getEmpInfoTelphone());
		info.setEmpInfoLabManaPrincipal(eForm.getEmpInfoLabManaPrincipal());
		info.setEmpInfoCellPhone1(eForm.getEmpInfoCellPhone1());
		info.setEmpInfoTelphone1(eForm.getEmpInfoTelphone1());
		info.setEmpInfoManHourRegu(eForm.getEmpInfoManHourRegu());
		info.setEmpInfoLowsalaLevel(eForm.getEmpInfoLowsalaLevel());
		info.setEmpInfoAllWorkNum(eForm.getEmpInfoAllWorkNum());
		info.setEmpInfoTownWorkNum(eForm.getEmpInfoTownWorkNum());
		info.setEmpInfoCountryWorkNum(eForm.getEmpInfoCountryWorkNum());
		info.setEmpInfoContractedNum(eForm.getEmpInfoContractedNum());
		info.setEmpInfoNcontractedNum(eForm.getEmpInfoNcontractedNum());
		info.setEmpInfoGoonContractNum(eForm.getEmpInfoGoonContractNum());
		info.setEmpInfoEmpInjInsNum(eForm.getEmpInfoEmpInjInsNum());
		info.setEmpInfoEndowmentInsNum(eForm.getEmpInfoEndowmentInsNum());
		info.setEmpInfoManNum(eForm.getEmpInfoManNum());
		info.setEmpInfoWomanNum(eForm.getEmpInfoWomanNum());
		info.setEmpInfoMedicalInsNum(eForm.getEmpInfoMedicalInsNum());
		info.setEmpInfoUnempInsNum(eForm.getEmpInfoUnempInsNum());
		info.setEmpInfoMaterityInsNum(eForm.getEmpInfoMaterityInsNum());
		info.setEmpInfoNoInsNum(eForm.getEmpInfoNoInsNum());
		info.setEmpInfoIfOrgDealDisputed(eForm.getEmpInfoIfOrgDealDisputed());
		info.setEmpInfoIfContract(eForm.getEmpInfoIfContract());
		info.setEmpInfoHonesLevel(eForm.getEmpInfoHonesLevel());
		dao.merge(info);
		return new ActionForward("/unitinfo.do?method=detail", true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoForm eForm = (EmployerInfoForm)form;
		EmployerInfo info = new EmployerInfo();
		info.setEmpInfoName(eForm.getEmpInfoName());
		info.setEmpInfoCode(eForm.getEmpInfoCode());
		info.setEmpInfoBusinessRegisOrgan(eForm.getEmpInfoBusinessRegisOrgan());
		info.setEmpInfoShopCard(eForm.getEmpInfoShopCard());
		info.setEmpInfoLandtaxUrl(eForm.getEmpInfoLandtaxUrl());
		info.setEmpInfoLandtaxRegisOrgan(eForm.getEmpInfoLandtaxRegisOrgan());
		info.setEmpInfoShopUrl(eForm.getEmpInfoShopUrl());
		info.setEmpInfoBankOfDeposit(eForm.getEmpInfoBankOfDeposit());
		info.setEmpInfoBankAccount(eForm.getEmpInfoBankAccount());
		info.setEmpInfoUnitType(eForm.getEmpInfoUnitType());
		info.setEmpInfoIndustry(eForm.getEmpInfoIndustry());
		info.setEmpInfoPlaceOrgan(eForm.getEmpInfoPlaceOrgan());
		info.setEmpInfoPlaceAddress(eForm.getEmpInfoPlaceAddress());
		info.setEmpInfoCorporateRepresent(eForm.getEmpInfoCorporateRepresent());
		info.setEmpInfoRepresentIdNum(eForm.getEmpInfoRepresentIdNum());
		info.setEmpInfoCellPhone(eForm.getEmpInfoCellPhone());
		info.setEmpInfoTelphone(eForm.getEmpInfoTelphone());
		info.setEmpInfoLabManaPrincipal(eForm.getEmpInfoLabManaPrincipal());
		info.setEmpInfoCellPhone1(eForm.getEmpInfoCellPhone1());
		info.setEmpInfoTelphone1(eForm.getEmpInfoTelphone1());
		info.setEmpInfoManHourRegu(eForm.getEmpInfoManHourRegu());
		info.setEmpInfoLowsalaLevel(eForm.getEmpInfoLowsalaLevel());
		info.setEmpInfoAllWorkNum(eForm.getEmpInfoAllWorkNum());
		info.setEmpInfoManNum(eForm.getEmpInfoManNum());
		info.setEmpInfoWomanNum(eForm.getEmpInfoWomanNum());
		info.setEmpInfoTownWorkNum(eForm.getEmpInfoTownWorkNum());
		info.setEmpInfoCountryWorkNum(eForm.getEmpInfoCountryWorkNum());
		info.setEmpInfoContractedNum(eForm.getEmpInfoContractedNum());
		info.setEmpInfoNcontractedNum(eForm.getEmpInfoNcontractedNum());
		info.setEmpInfoGoonContractNum(eForm.getEmpInfoGoonContractNum());
		info.setEmpInfoEmpInjInsNum(eForm.getEmpInfoEmpInjInsNum());
		info.setEmpInfoEndowmentInsNum(eForm.getEmpInfoEndowmentInsNum());
		info.setEmpInfoMedicalInsNum(eForm.getEmpInfoMedicalInsNum());
		info.setEmpInfoUnempInsNum(eForm.getEmpInfoUnempInsNum());
		info.setEmpInfoMaterityInsNum(eForm.getEmpInfoMaterityInsNum());
		info.setEmpInfoNoInsNum(eForm.getEmpInfoNoInsNum());
		info.setEmpInfoIfOrgDealDisputed(eForm.getEmpInfoIfOrgDealDisputed());
		info.setEmpInfoHonesLevel(eForm.getEmpInfoHonesLevel());
		info.setEmpInfoIfContract(eForm.getEmpInfoIfContract());
		info.setEmpInfoDelOper("9");
		UnitInfoDAO dao = new UnitInfoDAO(request, response);
		dao.save(info);
//		把单位信息插入单位用户提交管理申请表中�
		UnitUserApply unitUserApply = new UnitUserApply();
		unitUserApply.setUUApplyState("0");
		unitUserApply.setUUApplyUnitId(info.getEmpInfoId());
		unitUserApply.setUUApplyUserId(SystemSession.getUnitUserId(request));
		dao.saveApply(unitUserApply);
		return mapping.findForward("success");
	//	return new ActionForward("/unitinfo.do?method=detail", true);
	}
	
	public ActionForward addSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		UnitInfoDAO dao = new UnitInfoDAO(request, response);
		UnitUser user = dao.findId(SystemSession.getUnitUserId(request));
	//	user.setUnitUserUnitId(request.getParameter("supvInfoUnitId"));
		dao.updateUnit(user);
		return new ActionForward("/unitinfo.do?method=detail", true);
	}
	
	
	//台帐
	public ActionForward doc1(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		List list = dao.findEmployer(request.getParameter("eid"));
		if(list != null && list.size() > 0){
			request.setAttribute("eInfo", list.get(0));
			request.setAttribute("historyList", dao.findEmployerHistoryList(request.getParameter("eid")));
		}
		return mapping.findForward("doc1");
	}
	
	//合同一览表
	public ActionForward doc2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		List list = dao.findEmployerContract(request.getParameter("eid"));
		request.setAttribute("list", list);
		return mapping.findForward("doc2");
	}
	
	//续定
	public ActionForward doc3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		List list = dao.findEmployer(request.getParameter("eid"));
		if(list != null && list.size() > 0){
			request.setAttribute("eInfo", list.get(0));
			request.setAttribute("cList", dao.findGoOnEmployerContract(request.getParameter("eid")));
		}
		return mapping.findForward("doc3");
	}
	
	//解除
	public ActionForward doc4(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		List list = dao.findEmployer(request.getParameter("eid"));
		if(list != null && list.size() > 0){
			request.setAttribute("eInfo", list.get(0));
			request.setAttribute("cList", dao.findEndEmployerContract(request.getParameter("eid")));
		}
		return mapping.findForward("doc4");
	}

	
}